package gui.panels;

import com.google.common.eventbus.Subscribe;
import contracts.requests.LoginRequest;
import contracts.responses.LoginResponse;
import eventbus.PageEventBus;
import eventbus.events.UpdatePanelEvent;
import gui.BasePanel;
import network.websocket.WSClientSender;
import state.SessionState;
import util.DialogHelper;
import util.StringValidator;

import javax.swing.*;
import java.awt.*;


public class LoginPanel extends BasePanel {

    JLabel titleLabel = new JLabel("聊天室");

    JLabel usernameLabel = new JLabel("用户名:");
    JTextField usernameInput = new JTextField();

    JLabel passwordLabel = new JLabel("密码:");
    JPasswordField passwordInput = new JPasswordField();

    JButton loginButton = new JButton("登录");
    JButton registerButton = new JButton("注册");

    public LoginPanel() {
        this.setBackground(Color.LIGHT_GRAY);
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 40));
        titleLabel.setForeground(Color.blue);
        titleLabel.setBounds(340, 50, 300, 50);

        usernameLabel.setFont(new Font("微软雅黑", Font.BOLD, 25));
        usernameLabel.setForeground(Color.red);
        usernameLabel.setBounds(200, 150, 100, 32);
        usernameInput.setBounds(330, 150, 230, 32);

        passwordLabel.setFont(new Font("微软雅黑", Font.BOLD, 25));
        passwordLabel.setForeground(Color.red);
        passwordLabel.setBounds(200, 200, 100, 32);
        passwordInput.setBounds(330, 200, 230, 32);


        loginButton.setBackground(Color.green);
        loginButton.setBounds(300, 300, 200, 32);
        registerButton.setBackground(Color.YELLOW);
        registerButton.setBounds(300, 350, 200, 32);

        this.add(titleLabel);
        this.add(usernameLabel);
        this.add(usernameInput);
        this.add(passwordLabel);
        this.add(passwordInput);
        this.add(loginButton);
        this.add(registerButton);
        registerButton.addActionListener(e -> {
            PageEventBus.publish(UpdatePanelEvent.create(RegisterPanel.class));
        });

        loginButton.addActionListener(e -> {
            String userName = usernameInput.getText().trim();
            String password = new String(passwordInput.getPassword());
            if (StringValidator.isBlank(userName)) {
                DialogHelper.showWaringDialog("用户名不能为空");
                return;
            }
            if (StringValidator.isBlank(password)) {
                DialogHelper.showWaringDialog("密码不能为空");
                return;
            }
            //组装请求实体
            LoginRequest request = new LoginRequest(userName, password);
            WSClientSender.send(request);
        });

    }

    @Subscribe
    public void handleLoginResponse(LoginResponse e) {
        if (e.success) {
            DialogHelper.showSuccessDialog("登录成功");
            PageEventBus.publish(UpdatePanelEvent.create(ChatPanel.class));
            SessionState.Instance.setToken(e.token);
            SessionState.Instance.setConnId(e.connectId);
            SessionState.Instance.setLoginSuccess(true);
        } else {
            DialogHelper.showWaringDialog(e.errorMessage);
        }
    }


    @Override
    public BasePanel clearAllInput() {
        usernameInput.setText("");
        passwordInput.setText("");
        return this;
    }
}
